﻿'Justin Bassett

Public Class frmGame
    Private playerName As String
    Private multipl As Double
    Private Operation As Char
    Private QuestionScore As Integer
    Private TotalScore As Integer
    Private QuestionsAnswered As Integer
    Private topNumber As Integer
    Private bottomNumber As Integer

    Public Sub passInformations(ByVal Name As String, ByRef Multiplier As Double, ByVal Operat As Integer)
        Randomize()
        playerName = Name
        multipl = Multiplier
        Select Case Operat
            Case 0
                Operation = "+"
            Case 1
                Operation = "-"
            Case 2
                Operation = "*"
            Case 3
                Operation = "/"
            Case Else
                MessageBox.Show("ERROR", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Select
        lblOperator.Text = Operation
        TotalScore = 0
        QuestionsAnswered = 0
        newQuestion()
        timer.Enabled = True
    End Sub

    Private Sub timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer.Tick
        QuestionScore -= 25
        If QuestionScore < 0 Then
            QuestionScore = 0
        End If
        updateLabels()
    End Sub

    Private Sub newQuestion()
        QuestionScore = 1000
        If multipl = 0.75 Then                      'Easy
            If Operation = "/" Then
                Do
                    topNumber = Int(Rnd() * 6)
                    bottomNumber = Int(Rnd() * 5 + 1)
                Loop While (topNumber Mod bottomNumber <> 0)
            ElseIf Operation = "-" Then
                Do
                    topNumber = Int(Rnd() * 6)
                    bottomNumber = Int(Rnd() * 6)
                Loop While (topNumber < bottomNumber)
            Else
                topNumber = Int(Rnd() * 6)
                bottomNumber = Int(Rnd() * 6)
            End If
        ElseIf multipl = 1 Then                     'Medium
            topNumber = Int(Rnd() * 10)
            bottomNumber = Int(Rnd() * 10)
        ElseIf multipl = 1.5 Then                   'Hard
            topNumber = Int(Rnd() * 13)
            bottomNumber = Int(Rnd() * 13)
        Else                                        'Expert
            topNumber = Int(Rnd() * 25 - 12)
            bottomNumber = Int(Rnd() * 25 - 12)
        End If
        'topNumber = 0
        'bottomNumber = 0
        If Operation = "/" And bottomNumber = 0 Then    'Check for division by zero!
            newQuestion()
        End If
        Me.lblNumbers.Text = topNumber & vbNewLine & bottomNumber
    End Sub

    Public Sub updateLabels()
        If QuestionScore < 0 Then
            QuestionScore = 0
        End If
        Me.lblQuestionScore.Text = "Question: " & QuestionScore
        Me.lblTotalScore.Text = "Score: " & TotalScore
    End Sub

    Private Sub wrongAnswerTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles wrongAnswerTimer.Tick
        timer.Start()
        wrongAnswerTimer.Stop()
    End Sub

    Private Sub txtAnswer_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAnswer.KeyPress
        If e.KeyChar = vbCr Then
            Dim correctNumber As Double
            Dim userGuess As Double = Val(Me.txtAnswer.Text)
            If Me.txtAnswer.Text = Nothing Then
                Exit Sub
            End If
            Select Case Operation
                Case "+"
                    correctNumber = topNumber + bottomNumber
                Case "-"
                    correctNumber = topNumber - bottomNumber
                Case "*"
                    correctNumber = topNumber * bottomNumber
                Case "/"
                    correctNumber = topNumber / bottomNumber
            End Select
            If Math.Abs(correctNumber - userGuess) < 0.001 Then
                TotalScore += QuestionScore
                QuestionsAnswered += 1
                newQuestion()
            ElseIf QuestionScore > 0 Then
                QuestionScore -= 200
                timer.Stop()
                wrongAnswerTimer.Start()
                MessageBox.Show("Incorrect Guess", "Wrong!")
            Else
                timer.Stop()
                wrongAnswerTimer.Start()
                MessageBox.Show("You get no points for this question.", "Moving On")
                QuestionsAnswered += 1
                newQuestion()
            End If
            If QuestionsAnswered >= 10 Then
                EndGame()
            End If
            Me.txtAnswer.Text = Nothing
            updateLabels()
        End If
End Sub

    Private Sub EndGame()
        Me.Hide()
        TotalScore *= multipl
        MessageBox.Show("Congratulations " & playerName & " you got a score of " & TotalScore, TotalScore.ToString)
        frmHighScores.addToHighScore(playerName, TotalScore)
    End Sub

    Private Sub frmGame_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        frmMultiplicationPractice.Show()
        frmHighScores.Show()
    End Sub
End Class